<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .container {
            width: 100%;
            height: 100vh;
            background-color: pink;
            padding: 10px;
            box-sizing: border-box;
        }

        .content {
            width: 100%;
            height: 75%;
            background-color: #fff;
            padding: 20px;
            box-sizing: border-box;
        }

        .input {
            height: 20%;
            width: 100%;
            position: relative;
            margin-top: 20px;
        }

        .input textarea,
        .input button {
            position: absolute;
            resize: none;
        }

        .input textarea {
            width: 100%;
            height: 100%;
            padding: 10px;
            box-sizing: border-box;
        }

        .input button {
            right: 10px;
            bottom: 10px;
            width: 100px;
            height: 50px;
            background-color: blue;
            color: #fff;
            font-size: 20px
        }

        .message {
            color: brown;
            font-weight: bolder;
            font-size: 30px;
            margin-right: 20px;
        }

        .datetime {
            color: #000;
            font-size: 12px;
        }
    </style>
</head>

<body>
    <div class="container">
        <!-- 聊天区 -->
        <div class="content"></div>
        <!-- 输入区 -->
        <div class="input">
            <textarea name="" id="contentArea"></textarea>
            <button id="btnSend">发送</button>
        </div>
    </div>
    <script>

        const btnDom = document.querySelector('#btnSend') //发送按钮对象
        const textareaDom = document.querySelector('#contentArea') // 输入区对象
        const contentDom = document.querySelector('.content') //聊天区对象
        enableBtn()
        textareaDom.addEventListener('input', (event) => {
            // console.log(event.target.value, '---', textareaDom.value);
            enableBtn() // 控制按钮是否能点击
        })
        btnDom.addEventListener('click', () => {
            const content = textareaDom.value //聊天信息
            const p = document.createElement('p')
            p.innerText = content
            contentDom.append(p)
            textareaDom.value = ''
        })
        textareaDom.addEventListener('keydown', (event) => {
            if (event.key === 'Enter') {
                sendMsg()
            }
        })
        function enableBtn() {
            if (textareaDom.value) {
                // 删除disabled属性
                btnDom.removeAttribute('disabled')
            } else {
                // 加上属性
                btnDom.setAttribute('disabled', true)
            }
        }
        function sendMsg() {
            const content = textareaDom.value //聊天信息
            const p = document.createElement('p')
            p.innerText = content
            p.innerHTML = `<span class='message'>${content}</span><span class='datetime'>${new Date().toLocaleString()}</span>`
            contentDom.append(p)
            textareaDom.value = ''
        }
    </script>
</body>

</html>